package service.Impl;

import dao.mapper.OrderStateMapper;
import org.apache.ibatis.session.SqlSession;
import service.IOrderStateService;
import utils.SqlSessionUtil;
import vo.OrderState;

import java.util.List;

public class IOrderStateServiceImpl implements IOrderStateService {
    SqlSession sqlSession = new SqlSessionUtil().sqlSession;
    OrderStateMapper orderStateMapper = sqlSession.getMapper(OrderStateMapper.class);
    IBookServiceImpl bookService = new IBookServiceImpl();
    @Override
    public void updateOrderState(int id,String state) {
        orderStateMapper.updateOrderState(id, state);
        sqlSession.commit();
        if(state.equals("退货成功")){
            OrderState orderState = orderStateMapper.getOrderStateById(id);
            bookService.changeInventory(orderState.getOrderId(), "add");
        }
    }

    @Override
    public List<OrderState> getAllOrderState() {
        List<OrderState> orderStates = orderStateMapper.getAllOrderState();
        return orderStates;
    }

    @Override
    public void addOrderState(int orderId) {
        OrderState orderState=new OrderState();
        orderState.setOrderId(orderId);
        orderStateMapper.addOrderState(orderState);

        sqlSession.commit();
    }

    @Override
    public void updateOrderStateByOrderId(int orderId,String orderState) {
        if(orderId==0){
            return;
        }else {
            orderStateMapper.updateOrderStateByOrderId(orderId,orderState);
        }
        sqlSession.commit();
    }

    @Override
    public OrderState selectOrderStateByOrderId(int orderId) {
        return orderStateMapper.selectOrderStateByOrderId(orderId);
    }
}
